#include "MySQLConnector.h"
#include "../include/Configuration.h"
using namespace log4cpp;
MySQLConnector* MySQLConnector::_pinstance = nullptr;
MYSQL * MySQLConnector::_conn = nullptr;
MYSQL_RES * MySQLConnector::_result = nullptr;
mutex MySQLConnector::_mux;
MySQLConnector::MySQLConnector()
{
    //构造函数初始化连接
    _conn = mysql_init(nullptr);
    if(_conn == NULL)
    {
        LOG_ERROR("mysql_init error");
    }
}
MySQLConnector::~MySQLConnector()
{
    if(_conn !=nullptr)
    {
        mysql_close(_conn);
        _conn = nullptr;
    }
    if(_result != nullptr)
    {
        mysql_free_result(_result);
        _result = nullptr;
    }
}
MySQLConnector * MySQLConnector::getInstance()
{
    lock_guard<mutex>lock(_mux);
    if(_pinstance == nullptr)
    {
        _pinstance = new MySQLConnector();
    }
    return _pinstance;
}
MYSQL_RES*  MySQLConnector::getResult()
{
    if(_result != nullptr)
    {
        return _result;
    }
    else
    {
        LOG_ERROR("MYSQL_RES is nullptr");
        return nullptr;
    }
}
void MySQLConnector::destory()
{
    lock_guard<mutex>lock(_mux);
    if(_conn != nullptr)
    {
        mysql_close(_conn);
        _conn = nullptr;
    }
    if(_result != nullptr)
    {
        mysql_free_result(_result);
        _result = nullptr;
    }
    if(_pinstance )
    {
        delete _pinstance;
        _pinstance = nullptr;
    }
}

void MySQLConnector::query(const string & sql)
{
    //释放旧结果集
    if(_result != nullptr)
    {
        mysql_free_result(_result);
        _result = nullptr;
    }
    if(_conn != nullptr)
    {
        if(mysql_query(_conn, sql.c_str()))
        {
            LOG_ERROR("mysql_query");
            return ;
        }
        _result = mysql_store_result(_conn);
        if(_result == nullptr)
        {
            LOG_ERROR("mysql_store_result");
        }
    }
}

bool MySQLConnector::connect(const string &localhost,
                             const string &username,
                             const string &password,
                             const string &database)
{
    if(_conn != nullptr)
    {
        if(mysql_real_connect(_conn, localhost.c_str(), username.c_str() , 
                              password.c_str(), database.c_str(),
                              0, NULL, 0) == NULL)
        {
            mysql_close(_conn);
            _conn = nullptr;
            LOG_ERROR("mysql_real_connect");
            return false;
        }

    }
    else
    {
        LOG_ERROR("mysql conn is nullptr");
        return false;
    }
    return true;
}

bool MySQLConnector::execute(const string &sql)
{
        //释放旧结果集
        if(_result != nullptr)
        {
            mysql_free_result(_result);
            _result = nullptr;
        }
    if(_conn != nullptr)
    {
        if(mysql_query(_conn, sql.c_str()))
        {
            LOG_ERROR("execute error");
            return false;
        }

    }
    else
    {
        LOG_ERROR("mysql conn is nullptr");
        return false;
    }
    return true;
}
